<?php 
namespace Teacher\Controller;

use Admin\Controller\AdminbaseController;
class TeacheradminController extends AdminbaseController{
	
	protected $m;
	
	function _initialize() {
		parent::_initialize();
		$this->m = D('Users');
	}
	
	public function index(){
		$this->uid = $this->curUser['uid'];
		if(!$_GET['status']){
			$_GET['status'] = 0;
		}		
		$this->display();
	}
	
	
	public function edit(){
		
		if($_GET['id']){
			$this->info = $this->m->relation(true)->find($_GET['id']);
		}
		$this->display();
	}
	
	public function edit_post(){
		if(IS_POST){
			$data_user = array(
					'realname' => $_POST['realname'],
					'mobile' => $_POST['mobile'],
					'username' => $_POST['username'],
					'update_time' =>Date('Y-m-d H:i:s'),
			);
			if($_POST['uid']){
				if($_POST['password']){
					$password = md5($_POST['password']);
					$data_user['password'] =$password;
				}
				$data_user['uid'] = $_POST['uid'];
				$this->m->create($data_user);
				if($this->m->save()){
					$this->save_english($_POST['uid'],$_POST['english']);
					$this->success('更新成功');
				}else{
					$this->error('更新失败');
				}	
				
			}else{
				if($_POST['password']){
					$password = md5($_POST['password']);
				}else{
					$password = md5('666666');
				}
				$data_user['password'] =$password;
				$data_user['type'] = 'add';
				$data_user['role'] = 'teacher';
				$data_user['create_time'] = Date('Y-m-d H:i:s');
				$this->m->create($data_user);
				$id = $this->m->add();
				if($id){
					$this->save_english($id,$_POST['english']);
					$this->success('添加成功');
				}else{
					$this->error('添加失败');
				}
			}
		}
	}
	
	public function save_english($uid,$englih){
		$m = M('UsersEnglish');
		$data = $m->where(array('uid'=>$uid))->find();
		if($data){
			$m->where(array('uid'=>$uid))->save(array('english'=>$englih));
		}else{
			$m->add(array('uid'=>$uid,'english'=>$englih));
		}
	}
	
	public function check(){
		$t = I('get.t');
		if($t === 'mobile'){
			$value = $_REQUEST['mobile'];
			$str_ok = '';
			$str_error = '手机号码已存在';
		}else if($t === 'username'){
			$value = $_REQUEST['username'];
			$str_ok = '';
			$str_error = '用户名已存在';
		}
		$uid = I('get.uid');
		if($uid){
			$map['uid'] = array('neq',$uid);
		}
		$map[$t] = $value;
		if(!$this->m->where($map)->find()){
			$data['ok'] = $str_ok;	
		}else{
			$data['error'] = $str_error;
		}
		$this->ajaxReturn($data,'JSON');
	}
	
	public function del(){
		$id = array_unique((array)I('ids',0));
		if ( empty($id) ) {
			$this->error('请选择要操作的数据!');
		}
		$map = array('uid' => array('in', $id));
		if($this->m->where($map)->delete()){
			$this->success('删除成功',U('index'));
		} else {
			$this->error('删除失败！');
		}
	}
	
	public function listorders() {
		$status = parent::_listorders($this->m);
		if ($status) {
			$this->success("排序更新成功！",U('index'));
		} else {
			$this->error("排序更新失败！");
		}
	}
	
	public function changestatus(){
		$data[$this->m->getPk()] = $_REQUEST['id'];
		$data['status'] = $_REQUEST['status'];
		$data['update_time'] = date('Y-m-d H:i:s');
		if($this->m->save($data)){
			$this->success('状态更新成功');
		}else{
			$this->error('状态更新失败');
		}
	}
	
	public function getstuname($lsrids){
		$students = $this->lst->where(array('id'=>array('in',$lsrids)))->getField('student',true);
		return implode(',',$students);
	}
}

?>